#include "LoopSimul.h"

#include "TrajSimul.h"
#include "LoopSave.h"

#include <iostream>

LoopSimul::LoopSimul()
{

}

LoopSimul::~LoopSimul()
{

}

MultiLayer LoopSimul::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
  LoopSave().initSave(output);
  Vector3 downMag=multiLayer[multiLayer.getSolveLayer()].getMag();
  MultiLayer downMultiLayer=multiLayer;
  downMag.mult(-1);
  downMultiLayer[multiLayer.getSolveLayer()].setMag(downMag);
  for(field->setLevel(params.getField()["min"]);field->getLevel(clock.getTimeStamp())<=params.getField()["max"];field->setLevel(field->getLevel(clock.getTimeStamp())+params.getField()["step"]))
    {
      output.setSave(false);
      MultiLayer result=TrajSimul().solve(clock,output,field,current,multiLayer,params);
      LoopSave().save(clock,output,field,result);
    }
  for(field->setLevel(params.getField()["max"]);field->getLevel(clock.getTimeStamp())>=params.getField()["min"];field->setLevel(field->getLevel(clock.getTimeStamp())-params.getField()["step"]))
    {
      output.setSave(false);
      MultiLayer result=TrajSimul().solve(clock,output,field,current,downMultiLayer,params);
      LoopSave().save(clock,output,field,result);
    }

  return multiLayer;
}
